﻿using System;
using System.Linq;
using System.ServiceProcess;
using System.Windows;
using System.Reflection;

namespace BeerManagementSystem
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class Launcher
    {
        private UserManager _userManagerWindow;
        private MonthlyReport _monthlyReportWindow;
        private BeerdocterControl _beerdocterControl;
        private Controller _controller;

        public Launcher()
        {
            //StartService("MSSQL$SQLEXPRESS", 2000);
            InitializeComponent();
            WebConnectedOnline.IsChecked = Properties.Settings.Default.WebConnectorStatus;
            Title = String.Format("BMS {0} - Launcher by Jonathan & Malte", Assembly.GetExecutingAssembly().GetName().Version);
            LogWriter.Write("Launcher gestartet.", LogWriter.LogLevel.Status, this);
            Show();
            _controller = Controller.Instance;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            _controller.OpenCheckIn();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _controller.SetWebStatus(0);
            LogWriter.Write("BMS shutting down.", LogWriter.LogLevel.Status, this);
            _controller.Dispose();
            Application.Current.Shutdown();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _userManagerWindow = new UserManager();
                _userManagerWindow.Show();
            }
            catch (Exception)
            {
                //log.WriteMessage(ex.StackTrace);
            }
            
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            _monthlyReportWindow = new MonthlyReport();
            _monthlyReportWindow.Show();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            var admin = new Administration(_controller);

            admin.Show();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            _beerdocterControl= new BeerdocterControl();
            _beerdocterControl.Show();
            

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            _controller.InitializeEmulatedCardReader();
        }
        
       
        private void HandleCheck(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.WebConnectorStatus = true;
            WebConnectedOnline.Content = "Online";
            Properties.Settings.Default.Save();
        }

        private void HandleUnchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.WebConnectorStatus = false;
            WebConnectedOnline.Content = "Offline";
            Properties.Settings.Default.Save();
        }

        public static void StartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (var serviceController in services)
            {
                Console.WriteLine(serviceController.ServiceName);
            }

            if (IsServiceInstalled(serviceName))
            {
                var service = new ServiceController(serviceName);
                string svcStatus = service.Status.ToString();
                if (svcStatus != "Running")
                try
                {
                    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }
        }

        public static bool IsServiceInstalled(string serviceName)
        {
            // get list of Windows services
            ServiceController[] services = ServiceController.GetServices();

            // try to find service name
            return services.Any(service => service.ServiceName == serviceName);
        }
        
    }
}
